//
//  HomeHeadView.swift
//  Stock_Stategy
//
//  Created by MayerF on 2017/9/11.
//  Copyright © 2017年 MayerF. All rights reserved.
//

import UIKit

@objc protocol HomeHeadViewDelegate {
    func onClick(clickedBtn: UIButton)
}

class HomeHeadView: UIView {
    let stategy = UIButton()
    let newDynamic = UIButton()
    let moreBtn = UIButton()
    let arrow = UIImageView()
    let line = UIView()
    let selectedLine = UIView()
    @objc var delegete: HomeHeadViewDelegate?
    
    //MARK: -
    //MARK: init
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        initCommon()
        initConstriants()
    }
    private func initCommon() {
        stategy.addTarget(self, action: #selector(stategyOnClick), for: .touchUpInside)
        newDynamic.addTarget(self, action: #selector(dynamicOnClick), for: .touchUpInside)
        moreBtn.addTarget(self, action: #selector(moreOnClick), for: .touchUpInside)
        
        line.backgroundColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)
        selectedLine.backgroundColor = UIColor(red: 255/255, green: 63/255, blue: 63/255, alpha: 1)
        stategy.setTitle("最优策略", for: .normal)
        newDynamic.setTitle("最新动态", for: .normal)
        moreBtn.setTitle("更多策略", for: .normal)
        arrow.contentMode = .center
        arrow.image = UIImage(named: ThemeManager.manager.getThemeImage(name: "arrow"))
        stategy.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        newDynamic.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        moreBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        stategy.setTitleColor(UIColor(red: 153/255, green: 153/255, blue: 153/255, alpha: 1), for: .normal)
        stategy.setTitleColor(UIColor(red: 255/255, green: 63/255, blue: 63/255, alpha: 1), for: .selected)
        newDynamic.setTitleColor(UIColor(red: 153/255, green: 153/255, blue: 153/255, alpha: 1), for: .normal)
        newDynamic.setTitleColor(UIColor(red: 255/255, green: 63/255, blue: 63/255, alpha: 1), for: .selected)
        moreBtn.setTitleColor(UIColor(red: 153/255, green: 153/255, blue: 153/255, alpha: 1), for: .normal)
        stategy.isSelected = true
    }
    private func initConstriants() {
        addSubview(stategy)
        addSubview(newDynamic)
        addSubview(moreBtn)
        addSubview(arrow)
        addSubview(line)
        addSubview(selectedLine)
        
        stategy.mas_makeConstraints { (make) in
            make?.top.bottom().equalTo()(self)
            make?.left.equalTo()(self)?.offset()(10)
            make?.right.equalTo()(self.newDynamic.mas_left)?.offset()(-20)
            make?.width.equalTo()(54)
        }
        newDynamic.mas_makeConstraints { (make) in
            make?.top.equalTo()(self)
            make?.bottom.equalTo()(self.stategy)
            make?.width.equalTo()(self.stategy)
        }
        moreBtn.mas_makeConstraints { (make) in
            make?.top.equalTo()(self)
            make?.bottom.equalTo()(self)
            make?.right.equalTo()(self.arrow.mas_left)
            make?.width.equalTo()(self.stategy)
        }
        arrow.mas_makeConstraints { (make) in
            make?.centerX.equalTo()(self.mas_right)?.offset()(-10-4)
            make?.centerY.equalTo()(self)
            make?.width.equalTo()(8)
            make?.height.equalTo()(15)
        }
        line.mas_makeConstraints { (make) in
            make?.bottom.equalTo()(self)
            make?.left.right().equalTo()(self)
            make?.height.equalTo()(0.75)
        }
        selectedLine.mas_makeConstraints { (make) in
            make?.bottom.equalTo()(self)
            make?.left.equalTo()(self)?.offset()(10)
            make?.width.equalTo()(self.stategy)
            make?.height.equalTo()(1.5)
        }
    }
    //MARK: - 
    //MARK: fuction
//    func updateSelectedLineConstraints(value: Float) {
//        var newValue = 100
//        selectedLine.mas_updateConstraints { (make) in
//            make?.left.equalTo()(self)?.offset()(value)
//        }
//        UIView.animate(withDuration: 0.25) {
//            self.layoutIfNeeded()
//        }
//    }
    @objc func updateTheme() {
        self.backgroundColor = ThemeManager.manager.getThemeNormalBackgroundColor()
        stategy.setTitleColor(ThemeManager.manager.getThemeHeaderFontColor(), for: .normal)
        newDynamic.setTitleColor(ThemeManager.manager.getThemeHeaderFontColor(), for: .normal)
        moreBtn.setTitleColor(ThemeManager.manager.getThemeHeaderFontColor(), for: .normal)
    }
    
    //MARK: -
    //MARK: aciton
    @objc private func stategyOnClick() {
        newDynamic.isSelected = false
        stategy.isSelected = true
        moreBtn.setTitle("更多策略", for: .normal)
        selectedLine.mas_updateConstraints { (make) in
            make?.left.equalTo()(self)?.offset()(10)
        }
        UIView.animate(withDuration: 0.25) { 
            self.layoutIfNeeded()
        }
        delegete?.onClick(clickedBtn: stategy)
    }
    @objc private func dynamicOnClick() {
        newDynamic.isSelected = true
        stategy.isSelected = false
        moreBtn.setTitle("更多动态", for: .normal)
        selectedLine.mas_updateConstraints { (make) in
            make?.left.equalTo()(self)?.offset()(10 + 54 + 18)
        }
        UIView.animate(withDuration: 0.25) {
            self.layoutIfNeeded()
        }
        delegete?.onClick(clickedBtn: newDynamic)
    }
    @objc private func moreOnClick() {
        delegete?.onClick(clickedBtn: moreBtn)
    }
    
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
